package com.hanhai.zrb.api.biz.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * 项目名称：zrb_v2   
 * 类名称：DateUtil   
 * 类描述： 时间类型获取
 * 创建人：luckcoffey@qq.com   
 * 创建时间：2015年5月27日 下午3:24:37   
 * 修改人：luckcoffey@qq.com   
 * 修改时间：2015年5月27日 下午3:24:37   
 * 修改备注：   
 * @Copyright: 2015 汉海集团有限公司 All rights reserved.
 * @version v2
 */
public class DateUtil {
	public static final int SECOND = 1000;
	public static final int MINUTE = SECOND * 60;
	public static final int HOUR = MINUTE * 60;
	public static final long DAY = HOUR * 24;
	public static final long WEEK = DAY * 7;
	public static final long YEAR = DAY * 356;

	final static public String FULL_ST_FORMAT = "yyyy-MM-dd HH:mm:ss";
	final static public String FULL_J_FORMAT = "yyyy/MM/dd HH:mm:ss";
	final static public String CURRENCY_ST_FORMAT = "yyyy-MM-dd HH:mm";
	final static public String CURRENCY_J_FORMAT = "yyyy/MM/dd HH:mm";
	final static public String DATA_FORMAT = "yyyyMMddHHmmss";
	final static public String ST_FORMAT = "yyyy-MM-dd HH:mm";
	final static public String ST_CN_FORMAT = "yyyy年MM月dd日 HH:mm";
	final static public String CN_FORMAT = "yy年MM月dd日HH:mm";
	final static public String DAY_FORMAT = "yyyy-MM-dd";
	final static public String SHORT_DATE_FORMAT = "yy-MM-dd";
	final static public String YEAR_FORMAT = "yyyy";
	final static public String YEAR_MONTH = "yyyy-MM";
	final static public String YEAR_MONTH_SHORT = "yyyyMM";

	public static String getDateTimeSequence() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT);
		return sdf.format(new Date());
	}

	public static String getDate(long second) {
		SimpleDateFormat sdf = new SimpleDateFormat(FULL_ST_FORMAT);
		return sdf.format(new Date(second));
	}


	
	public static String getToday() {
		SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
		return sdf.format(new Date());
	}
    //获取当前日期的的yyyy-MM-dd HH:mm:ss
	public static String getNow() {
		SimpleDateFormat sdf = new SimpleDateFormat(FULL_ST_FORMAT);
		return sdf.format(new Date());
	}

	public static String getShortDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(SHORT_DATE_FORMAT);
		return sdf.format(new Date());
	}

	public static String getYearMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH);
		return sdf.format(new Date());
	}

	public static String getYearMonthShort() {
		SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_SHORT);
		return sdf.format(new Date());
	}

	public static String getYear() {
		SimpleDateFormat sdf = new SimpleDateFormat(YEAR_FORMAT);
		return sdf.format(new Date());
	}

	/**
	 * 计算两个日期之间 的天数
	 * 
	 * @param strDate1
	 * @param strDate2
	 * @return
	 * @throws java.text.ParseException
	 */
	public static long getQuot(String time1, String time2)
			throws java.text.ParseException {
		long quot = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return quot;
	}

	/**
	 * 计算两个日期之间 相差多少天 多少小时多少分钟
	 * 
	 * @param strDate1
	 * @param strDate2
	 * @return
	 * @throws java.text.ParseException
	 */
	public static String getExactQuot(String time1, String time2)
			throws java.text.ParseException {
		long timeOut = 0;// 时间差
		long days = 0;// 天数
		long hours = 0;// 小时
		long minutes = 0;// 分钟
		long seconds = 0;// 秒钟
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			timeOut = date1.getTime() - date2.getTime();
			days = timeOut / DAY;// 相差天数
			timeOut = timeOut % DAY;// 计算天数后剩余的毫秒数
			hours = timeOut / HOUR;// 计算相差的小时
			timeOut = timeOut % HOUR;// 计算小时候的毫秒数
			minutes = timeOut / MINUTE;// 计算相差的分钟
		} catch (Exception e) {
			e.printStackTrace();
		}
		return days<1?"1":days+"";
	}

	public static String getExactQuots(String time2, String time1)//bbs时间差
			throws java.text.ParseException {
		long timeOut = 0;// 时间差
		long days = 0;// 天数
		long hours = 0;// 小时
		long minutes = 0;// 分钟
		long seconds = 0;// 秒钟
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			timeOut = date1.getTime() - date2.getTime();
			days = timeOut / DAY;// 相差天数
			timeOut = timeOut % DAY;// 计算天数后剩余的毫秒数
			hours = timeOut / HOUR;// 计算相差的小时
			timeOut = timeOut % HOUR;// 计算小时候的毫秒数
			minutes = timeOut / MINUTE;// 计算相差的分钟
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String s = null ;
		
		if( days > 0 ){
			s = days + "天前";
		}else{
			if( hours > 0 ){
				s =  hours + "小时前";
			}else{
				if( minutes > 0){
					s =  minutes + "分钟前";
				}
			}
		}
		
		
		return s;
		
		
	}
	
	
	public static Date getStringToDate(String parse, String inDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(parse);
		try {
			return sdf.parse(inDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getStringSysDate(String date) {

		String returnString = "";
		SimpleDateFormat strDateString = new SimpleDateFormat("yyyy-MM");

		// 传入日期
		Date strDate = null;
		try {
			strDate = strDateString.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 当前日期
		Calendar cal = Calendar.getInstance();
		Integer curryear = cal.get(Calendar.YEAR);
		Integer currmonth = cal.get(Calendar.MONTH) + 1;

		// 当前年月
		String currmonthStr = currmonth + "";
		if (currmonth < 10)
			currmonthStr = "0" + currmonth;

		String endDateString = curryear.toString() + "-" + currmonthStr;
		Date endDate = null;
		try {
			endDate = strDateString.parse(endDateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// 比较
		if (endDate.compareTo(strDate) < 0)
			returnString = "false";
		else
			returnString = "true";
		return returnString;
	}

	/**
	 * Date转换为String日期
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		DateFormat format = new SimpleDateFormat(DAY_FORMAT);
		return format.format(date);
	}
	
	/**
	 * Date转换为String日期
	 * @param date
	 * @return
	 */
	public static String dateToStringFullFormat(Date date) {
		DateFormat format = new SimpleDateFormat(FULL_ST_FORMAT);
		return format.format(date);
	}
	
	/**
	 * String转换为Date日期
	 * @param str
	 * @return
	 */
	public static Date stringToDate(String str) {  
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        Date date = null;  
        try {  
            date = format.parse(str);   
        } catch (ParseException e) {  
        }  
        return date;  
    }  

	/**
	 * 得到几天前的时间
	 * 
	 * @param day
	 * @return
	 */
	public static String getDateBefore(int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return dateToString(now.getTime());
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param day
	 * @return
	 */
	public static String getDateAfterByDay(int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return dateToString(now.getTime());
	}
	
	/**
	 * 得到几个月后的时间
	 * 
	 * @param day
	 * @return
	 */
	public static String getDateAfterByMonth(int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH) + day);
		return dateToString(now.getTime());
	}
	
	/**
	 * 得到几周后的时间
	 * 
	 * @param day
	 * @return
	 */
	public static String getDateAfterByWeek(int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		now.set(Calendar.DAY_OF_WEEK_IN_MONTH, now.get(Calendar.DAY_OF_WEEK_IN_MONTH) + day);
		return dateToString(now.getTime());
	}
	
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
		w = 0;
		return weekDays[w];
		}

	public static void main(String[] args) {
//		System.out.println(getDateBefore(3));
//		System.out.println(getDateAfterByMonth(1));
//		System.out.println(getDateAfterByWeek(1));
		System.out.println(getWeekOfDate(new Date()));
	}
}
